
package com.watchme.license.util;

import static java.lang.System.out;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

public class ListNets {

	public static void main(String args[]) throws SocketException {
		String ip = "172.16.64.182";
		String mac = "80-00-0B-56-3B-32";
		boolean ipflag = validatoIp(ip);
		// boolean flag = validatoIpAndMacAddress(ip, mac);
		// boolean macflag = validateMacAddress(mac);
		out.printf("validatoIPAddress ipflag=%s\n", ipflag);
		// out.printf("validatoMacAddress flag=%s\n", macflag);
		// out.printf("validatoIpAndMacAddress flag=%s\n", flag);
	}

	static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
		out.printf("Display name: %s\n", netint.getDisplayName());
		out.printf("Name: %s\n", netint.getName());
		byte[] mac = netint.getHardwareAddress();
		if (mac != null) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < mac.length; i++) {
				sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
			}
			System.out.println("mac=" + sb.toString());
		}

		Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
		for (InetAddress inetAddress : Collections.list(inetAddresses)) {
			out.printf("InetAddress: %s\n", inetAddress);
			System.out.println("InetAddress ip=" + inetAddress.getHostAddress());
		}
		out.printf("\n");
	}

	public static boolean validateMacAddress(String macAddress) throws SocketException {
		boolean returnFlag = false;
		Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
		for (NetworkInterface netint : Collections.list(nets)) {
			byte[] mac = netint.getHardwareAddress();
			StringBuilder sb = new StringBuilder();
			if (mac != null) {
				for (int i = 0; i < mac.length; i++) {
					sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
				}
				// System.out.println("mac=" + sb.toString());
			}
			if (sb.toString().equals(macAddress)) {
				returnFlag = true;
			}
		}
		return returnFlag;

	}

	public static boolean validatoIpAndMacAddress(String ipAddress, String macAddress) throws SocketException {
		boolean returnFlag = false;
		Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
		for (NetworkInterface netint : Collections.list(nets)) {
			byte[] mac = netint.getHardwareAddress();
			StringBuilder sb = new StringBuilder();
			if (mac != null) {
				for (int i = 0; i < mac.length; i++) {
					sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
				}
				System.out.println("mac=" + sb.toString());
			}
			if (sb.toString().equals(macAddress)) {
				Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
				String ip = "";
				for (InetAddress inetAddress : Collections.list(inetAddresses)) {
					ip = inetAddress.getHostAddress();
					System.out.println("InetAddress ip=" + inetAddress.getHostAddress());
					if (ipAddress.toString().equals(ip)) {
						returnFlag = true;
					}
				}
			}
		}
		return returnFlag;

	}

	public static boolean validatoIp(String ipAddress) throws SocketException {
		boolean returnFlag = false;
		Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
		for (NetworkInterface netint : Collections.list(nets)) {

			Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
			String ip = "";
			for (InetAddress inetAddress : Collections.list(inetAddresses)) {
				if (inetAddress instanceof Inet4Address) {
					ip = inetAddress.getHostAddress();
					// System.out.println("InetAddress ip=" +
					// inetAddress.getHostAddress());
					if (ipAddress.toString().equals(ip)) {
						returnFlag = true;
					}
				}
			}
		}
		return returnFlag;

	}

	public static boolean validatoLicenseId(String licenseId, Map<String, String> map) throws SocketException {

		String licenseType = map.get(licenseId);
		if (licenseType == null || "".equals(licenseType.trim())) {
			return false;
		}
		// 停用状态
		if ("0".equals(licenseType)) {
			return false;
		} else if ("1".equals(licenseType)) {
			return true;
		}
		return false;
	}
}